package edu.nf.ch08.web.api;

import edu.nf.ch08.common.ResultVO;
import edu.nf.ch08.entity.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author wangl
 * @date 2025/9/17
 */
@RestController
@RequestMapping("/api/v1")
//使用@Tag注解对当前的API进行简要描述
@Tag(name = "用户服务接口", description = "提供用户信息相关的API操作接口")
public class UserController {

    private final static List<User> users;

    static {
        User u1 = new User(1, "张三", 21);
        User u2 = new User(2,"李四", 20);
        User u3 = new User(3, "王五", 19);
        users = Arrays.asList(u1, u2, u3);
    }

    /**
     * 查询所有用户
     * （注意：请求url中不应该出现动词，由请求方法决定操作行为）
     * @return
     */
    @GetMapping("/users")
    @Operation(summary = "查询所有用户", description = "查询系统中所有用户的基本信息", method = "GET")
    /*@ApiResponses({
            @ApiResponse(responseCode = "200", description = "响应成功"),
            @ApiResponse(responseCode = "500", description = "查询失败，服务端错误")
    })*/
    public ResultVO<List<User>> listUsers() {
        return ResultVO.success(users);
    }

    @GetMapping("/user/{id}")
    //方法描述
    @Operation(summary = "查询用户", description = "根据用户ID查询基本信息", method = "GET")
    //参数描述
    @Parameters({
       //required属性表示必须传参
       //in表示参数通过什么途径传入（请求头、请求参数、路径参数等）
       @Parameter(name = "id", description = "用户ID"
               , required = true, in = ParameterIn.PATH)
    })
    //响应描述
    /*@ApiResponses({
            @ApiResponse(responseCode = "200", description = "响应成功"),
            @ApiResponse(responseCode = "500", description = "查询失败，服务端错误")
    })*/
    public ResultVO<User> getUser(@PathVariable("id") int uid) {
        for(User user : users) {
            if(uid == user.getId()) {
                return ResultVO.success(user);
            }
        }
        return ResultVO.error(404, "没有改用户信息");
    }

    @PostMapping("/user")
    @Operation(summary = "添加用户", description = "提交JSON格式的用户数据")
    @Parameters({
            @Parameter(name = "user", required = true)
    })
    public ResultVO addUser(@RequestBody User user) {
        return ResultVO.success();
    }
}